﻿using System;
using System.Windows.Forms;

namespace CC.Yacht.Windows.Forms
{
    public partial class LoginForm : Form
    {
        #region Constructor
        public LoginForm()
        {
            InitializeComponent();
            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            Text = Application.ProductName + " - Login";
            // ReSharper restore DoNotCallOverridableMethodsInConstructor

            if (!DesignMode)
            {
                _LinkLabelForgotPassword.Visible = false; //TODO: Remove this once forgot password functionality has been implemented
                _RegisterForm.Registering += _RegisterForm_Registering;

#if DEBUG
                _TextBoxEmail.Text = "ccharlton@pobox.com";
                _TextBoxPassword.Focus();
#endif
            }
        }
        #endregion

        #region Private Fields
        private readonly RegisterForm _RegisterForm = new RegisterForm();
        #endregion

        #region Public Events
        public event EventHandler<AuthenticateEventArgs> Authenticating;
        public event EventHandler<RegisterEventArgs> Registering;
        #endregion

        #region Public Properties
        public string Email
        {
            get { return _TextBoxEmail.Text; }
            set { _TextBoxEmail.Text = value; }
        }

        public string Password
        {
            get { return _TextBoxPassword.Text; }
            set { _TextBoxPassword.Text = value; }
        }
        #endregion

        #region Private Event Handlers
        private void _ButtonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void _ButtonLogin_Click(object sender, EventArgs e)
        {
            if (Authenticating == null)
            {
                throw new InvalidOperationException("Cannot login when the Authenticating event is not attached.");
            }

            _ButtonLogin.Enabled = false;

            Enabled = false;
            UseWaitCursor = true;
            
            Application.DoEvents();
            OnAuthenticating(new AuthenticateEventArgs(Email, Password));
        }

        private void _LinkLabelForgotPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            throw new NotImplementedException("TODO: Remove this once forgot password functionality has been implemented.");
        }

        private void _LinkLabelRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _RegisterForm.ShowDialog();
        }

        // ReSharper disable InconsistentNaming
        private void _RegisterForm_Registering(object sender, RegisterEventArgs e)
        // ReSharper restore InconsistentNaming
        {
            OnRegistering(e);
        }

        private void _TextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateButtonLoginEnabled();
        }
        #endregion

        #region Protected Methods
        protected void OnAuthenticating(AuthenticateEventArgs eventArgs)
        {
            if (Authenticating != null)
            {
                Authenticating(this, eventArgs);
            }
        }

        protected override void OnShown(EventArgs e)
        {
            UpdateButtonLoginEnabled();

            base.OnShown(e);
        }

        protected void OnRegistering(RegisterEventArgs eventArgs)
        {
            if (Registering != null)
            {
                Registering(this, eventArgs);
            }
        }
        #endregion

        #region Private Methods
        private void UpdateButtonLoginEnabled()
        {
            _ButtonLogin.Enabled = (_TextBoxEmail.TextLength > 0 && _TextBoxPassword.TextLength > 0);
        }
        #endregion

        #region Public Methods
        public void EndAuthenticate(bool authenticatedSuccessfully)
        {
            Enabled = true;
            UseWaitCursor = false;

            if (authenticatedSuccessfully)
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                UpdateButtonLoginEnabled();
            }

            //Application.DoEvents(); // NOTE: Is this really necessary -- We'll find out *grin*
        }

        public void EndRegister(bool registeredSuccessfully)
        {
            _RegisterForm.EndRegister(registeredSuccessfully);
        }

        public void Reset()
        {
            Email = string.Empty;
            Password = string.Empty;
        }
        #endregion
    }
}
